package com.demo.swingquickticket.swing.view.ticket;

import com.demo.swingquickticket.entity.Station;
import com.demo.swingquickticket.service.DataService;
import com.demo.swingquickticket.swing.layoutUtil.WindowUtil;
import com.demo.swingquickticket.swing.model.AppJListModel;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.util.List;
import java.util.Map;

@Data
@EqualsAndHashCode(callSuper = true)
public class StationDialog extends JDialog {

    private static final StationDialog INSTANCE = new StationDialog(new JFrame());

    private String result;
    JButton station1;
    JButton station2;
    JButton station3;
    JButton station4;
    JButton station5;
    JButton selected;
    JList<String> stationList;
    JTextField search;
    Map<String, List<Station>> stations;

    public StationDialog(JFrame frame) {
        super(frame, true);
        init();
        bind();
    }

    public static StationDialog getInstance() {
        INSTANCE.setVisible(true);
        return INSTANCE;
    }

    private void init() {
        this.setTitle("车站选择器");
        this.setSize(WindowUtil.getWindowWidth() / 4, WindowUtil.getWindowHeight() / 4);
        this.getContentPane().setLayout(new BorderLayout());

        //添加按钮的面板
        JPanel buttonPanel = new JPanel();

        this.station1 = new JButton("ABCDE");
        this.station2 = new JButton("FGHIJ");
        this.station3 = new JButton("KLMNO");
        this.station4 = new JButton("PQRST");
        this.station5 = new JButton("UVWXYZ");

        buttonPanel.add(station1);
        buttonPanel.add(station2);
        buttonPanel.add(station3);
        buttonPanel.add(station4);
        buttonPanel.add(station5);


        //列表面板
        JPanel stationPanel = new JPanel(new BorderLayout(5,5));

        //搜索输入框
        search = new JTextField();
        //数据列表
        stationList = new JList<>(new AppJListModel());
        JScrollPane station = new JScrollPane(stationList);
        setList("ABCDE");

        stationPanel.add(search, BorderLayout.NORTH);
        stationPanel.add(station, BorderLayout.CENTER);

        selected = new JButton("确定");

        this.getContentPane().add(buttonPanel, BorderLayout.NORTH);
        this.getContentPane().add(stationPanel, BorderLayout.CENTER);
        this.getContentPane().add(selected, BorderLayout.SOUTH);
    }

    private void bind() {
        station1.addActionListener(e -> setList(station1.getText()));
        station2.addActionListener(e -> setList(station2.getText()));
        station3.addActionListener(e -> setList(station3.getText()));
        station4.addActionListener(e -> setList(station4.getText()));
        station5.addActionListener(e -> setList(station5.getText()));
        selected.addActionListener(e -> {
            this.result = getStationList().getSelectedValue();
            INSTANCE.setVisible(false);
        });
        search.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                this.changedUpdate(e);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                this.changedUpdate(e);
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                AppJListModel model = (AppJListModel) stationList.getModel();
                model.setSearchData(search.getText());
                stationList.updateUI();
            }
        });

    }

    private void setList(String range) {
        if (stations == null) {
            stations = DataService.getStationList();
        }
        List<Station> station = stations.get(range);
        AppJListModel model = (AppJListModel) stationList.getModel();
        model.setData(Station.toListString(station), search.getText());
        stationList.updateUI();
    }
}
